﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

[RequireComponent(typeof (NetworkPosition), typeof (VelocitySimulate))]
public class SporePlayer : NetworkBehaviour {

    public Transform bulletSpawn;
    public GameObject bulletPrefab;

    public float sporePlayerSpeed;

    private Vector2 _direction;
    private Vector3 _lastPosition;

    private NetworkPosition _networkPosition;
    private VelocitySimulate _velocitySimulate;

    // Use this for initialization
    void Start() {
        // combine _networkPosition and _velocitySimulate
        _networkPosition = GetComponent<NetworkPosition>();
        _velocitySimulate = GetComponent<VelocitySimulate>();
        _velocitySimulate.SetPositionAction = v =>
        {
            _networkPosition.setPosition(v);
        };

        _lastPosition = transform.position;
    }

    // Update is called once per frame
    void Update() {
        if (!isLocalPlayer)
            return; // skip when it is in server

        float x, y;

        var touchDirection = BasicPanel.Instance.direction;
        if (touchDirection != Vector2.zero) {
            x = touchDirection.x;
            y = touchDirection.y;
        }
        else {
            var v = new Vector2(Input.GetAxis("Horizontal"),
                                Input.GetAxis("Vertical")).normalized;
            x = v.x;
            y = v.y;
        }

        CmdMoveInstruction(x * sporePlayerSpeed,
                           y * sporePlayerSpeed);

        if (Mathf.Abs(x) > float.Epsilon || Mathf.Abs(y) > float.Epsilon) {
            _direction = transform.position - _lastPosition;
            _lastPosition = transform.position;
        }
        bulletSpawn.right = _direction;

        if (Input.GetKeyDown(KeyCode.Space)) {
            CmdFire(bulletSpawn.position, bulletSpawn.rotation);
        }

        if (BasicPanel.Instance.toggleButtonA) {
            CmdCharge();
        }

        if (BasicPanel.Instance.toggleButtonB) {
            CmdShrink();
        }
    }

    [Command]
    public void CmdMoveInstruction(float x, float y) {
        _velocitySimulate.addVelocity(new Vector3(x, y));
    }

    /// <summary>
    ///     fire instruction, spawn the bullet
    /// </summary>
    [Command]
    public void CmdFire(Vector3 p, Quaternion r) {
        var bullet = (GameObject)Instantiate(bulletPrefab, p, r);
        NetworkServer.Spawn(bullet);
        // Destroy(bullet, 2.0f);
    }

    [Command]
    public void CmdCharge()
    {
        // TODO, temp use add 0.1f/tick for 0.5f seconds
        _velocitySimulate.addVelocity(_velocitySimulate.LastVelocity.normalized * 5f, 0.5f);
//        GetComponent<SnakeHead>().CmdChargeAll();
    }

    [Command]
    public void CmdShrink()
    {
       // GetComponent<VelocitySimulate>().addVelocity(new Vector3(0, -3));
    }

    [Command]
    public void CmdInit() {

    }

    public override void OnStartLocalPlayer() {
        // this method will only apply with your local player
        // TODO change the Skin or something, to distinct the player among others
        GetComponentInChildren<SporeSkin>().changeSkin(0);
        // TODO set the camera to this GameObject
        Camera.main.GetComponent<CameraMovement>().cameraAim = transform;
    }
}
